package xubo.wangcaifeng.love

import java.text.SimpleDateFormat
import java.util.Date

import com.alibaba.fastjson.JSON
import org.apache.spark.rdd.RDD


object Utils2 {
  def EachProviceFailCount(lines: RDD[String]) = {
    val provinceFailCount: RDD[(String, Int)] = lines.map(line => {
      val json = JSON.parseObject(line)
      val serviceName = json.get("serviceName").toString
      if (serviceName.equals("reChargeNotifyReq")) {
        val success = json.get("bussinessRst").toString
        if (success.equals() != "0000") {
          val proviceCode = json.get("provinceCode").toString
          val province: String = ProduceRules.MatchProvice(proviceCode)
          ("provinceFail", 1)
        } else {
          ("provinceFail", 0)
        }
      } else {
        ("provinceFail", 0)
      }
    })
    //写入到redis中
    provinceFailCount.foreachPartition(line=>{
      val jedis = JedisConnectionPool.getConnection()
      line.foreach(x=>
        jedis.incrBy(x._1,x._2)
      )
      jedis.close()
    })
  }
  def EachSecondMoney(lines: RDD[String]) = {
    val EachScondSuccessAndMoney: RDD[(String, String ,Double)] = lines.map(line => {
      val json = JSON.parseObject(line)
      val serviceName = json.get("serviceName").toString
      val success = json.get("bussinessRst").toString
      if (serviceName.equals("reChargeNotifyReq") && success.equals("0000")) {
        //截取日志时间
        val logTime = json.get("receiveNotifyTime").toString.take(12)
        //充值金额
        val money= json.get("chargefee").toString.toDouble
        (logTime.substring(0, 4) + "-" + logTime.substring(4, 6) + "-" + logTime.substring(6, 8) , logTime.substring(8, 10) + "-" + logTime.substring(10, 12), money)

      } else {
        ("time","0", 0)
      }
    })
    //写入到redis中
    EachScondSuccessAndMoney.foreachPartition(line=>{
      val jedis = JedisConnectionPool.getConnection()
      line.foreach(x=>
        jedis.hincrByFloat(x._1,x._2,x._3)
      )
      jedis.close()
    })
  }
  def EachSecondOrder(lines: RDD[String]) = {
    val EachScondSuccessAndMoney: RDD[(String, String ,Int)] = lines.map(line => {
      val json = JSON.parseObject(line)
      val serviceName = json.get("serviceName").toString
      val success = json.get("bussinessRst").toString
      if (serviceName.equals("reChargeNotifyReq") && success.equals("0000")) {
        //截取日志时间
        val logTime = json.get("receiveNotifyTime").toString.take(12)
        (logTime.substring(0, 4) + "-" + logTime.substring(4, 6) + "-" + logTime.substring(6, 8)+"-"+  logTime.substring(8, 10) , "minutes-"+logTime.substring(10, 12), 1)

      } else {
        ("order","0", 0)
      }
    })
    //写入到redis中
    EachScondSuccessAndMoney.foreachPartition(line=>{
      val jedis = JedisConnectionPool.getConnection()
      line.foreach(x=>
        jedis.hincrByFloat(x._1,x._2,x._3)
      )
      jedis.close()
    })
  }

}
